package servlet.admin.passage;

import entity.MixPFO;
import service.PassageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet( name = "AdminPassageFieldServlet")
public class AdminPassageFieldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PassageService service=new PassageService();
//        System.out.println(request.getParameter("fieldName"));

        String fieldId=request.getParameter("fieldId");
        request.setAttribute("fieldId" ,fieldId);

        String pageStr = request.getParameter("page");
        int page = (pageStr == null) ? 1 : Integer.valueOf(pageStr);
        request.setAttribute("page", page);
        int sum = service.getNumberOfFieldPassages(fieldId, 2);
        request.setAttribute("sum", sum);
        int num_every_page = 10;
        request.setAttribute("num_every_page", num_every_page);
        int last_page = (int) Math.ceil((double)sum / num_every_page);
        if(sum == 0)
            last_page = 1;
        request.setAttribute("last_page", last_page);


        List<MixPFO> list=service.passageByFieldId(fieldId, page, 10);
        request.setAttribute("list",list);
        request.getServletContext().getRequestDispatcher("/admin/passageFieldName.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
